#include "getprofileresponse.h"


#include <QDateTime>
#include <QDebug>


GetProfileResponse::GetProfileResponse(QObject *parent) :
    Network::JSONResponse(parent)
{
}


const Profile::ProfileData &GetProfileResponse::profile() const
{
    return m_profile;
}


void GetProfileResponse::parse(const QVariant &data)
{
    // qDebug() << "Get profile of friend:";
    // qDebug() << data;
    if (data.type() != QVariant::Map){
        setError(Response::Property, "The field of \"map\" isn't a map");
        return;
    }

    QVariantMap map = data.toMap();

    if(!map.contains("list")){
        setError(Response::Property, "The field of \"list\" is missing");
        return;
    }

    QVariant listVariant = map.value("list");
    if(listVariant.type() != QVariant::List) {
        setError(Response::Property, "The value of the field of \"list\" is incompatible");
        return;
    }

    QVariantList list = listVariant.toList();
    bool ok;
    foreach (QVariant item, list) {
        if (item.type() != QVariant::Map) {
            setError(Network::Response::Property, QString("The field of \"map\" isn't a map"));
            return;
        }

        QVariantMap variantMap = item.toMap();

        // Parse uid
        if (!variantMap.contains("uid")) {
            setError(Response::Property, "The field of \"uid\" is missing");
            return;
        }

        QVariant userIdVariant = variantMap.value("uid");
        qint64 userId = userIdVariant.toLongLong(&ok);
        if (!ok)
            return;

        // Parse userName
        if (!variantMap.contains("urn")) {
            setError(Response::Property, "The field of \"urn\" is missing");
            return;
        }

        QVariant userNameVariant = variantMap.value("urn");
        if (userNameVariant.type() != QVariant::String) {
            setError(Response::Property, "The value of the field of \"urn\" is incompatible");
            return;
        }

        QString userName = userNameVariant.toString();

        // Parse displayName
        if (!variantMap.contains("dpn")) {
            setError(Response::Property, "The field of \"dpn\" is missing");
            return;
        }

        QVariant displayNameVariant = variantMap.value("dpn");
        if (displayNameVariant.type() != QVariant::String) {
            setError(Response::Property, "The value of the field of \"dpn\" isn incompatible");
            return;
        }

        QString displayName = displayNameVariant.toString();

        // Parse firstName
        if (!variantMap.contains("fin")) {
            setError(Response::Property, "The field of \"fin\" is missing");
            return;
        }

        QVariant firstNameVariant = variantMap.value("fin");
        if (firstNameVariant.type() != QVariant::String) {
            setError(Response::Property, "The value of the field of \"fin\" is incompatible");
            return;
        }

        QString firstName = firstNameVariant.toString();

        // Parse lastName
        if (!variantMap.contains("lan")) {
            setError(Response::Property, "The field of \"lan\" is missing");
            return;
        }

        QVariant lastNameVariant = variantMap.value("lan");
        if (lastNameVariant.type() != QVariant::String) {
            setError(Response::Property, "The value of the field of \"lan\" isn incompatible");
            return;
        }

        QString lastName = lastNameVariant.toString();

        // Parse email
        if (!variantMap.contains("eml")) {
            setError(Response::Property, "The field of \"eml\" is missing");
            return;
        }

        QVariant emailVariant = variantMap.value("eml");
        if (emailVariant.type() != QVariant::String) {
            setError(Response::Property, "The value of the field of \"eml\" is incompatible");
            return;
        }

        QString email = emailVariant.toString();

        // Parse profileUrl
        if (!variantMap.contains("url")) {
            setError(Response::Property, "The field of \"url\" is missing");
            return;
        }

        QVariant profileUrlVariant = variantMap.value("url");
        if (profileUrlVariant.type() != QVariant::String) {
            setError(Response::Property, "The value of the field of \"url\" isn incompatible");
            return;
        }

        QString profileUrl = profileUrlVariant.toString();

        // Parse avatarUrl
        if (!variantMap.contains("avt")) {
            setError(Response::Property, "The field of \"avt\" is missing");
            return;
        }

        QVariant avatarUrlVariant = variantMap.value("avt");
        if (avatarUrlVariant.type() != QVariant::String) {
            setError(Response::Property, "The value of the field of \"avt\" isn incompatible");
            return;
        }

        QString avatarUrl = avatarUrlVariant.toString();

        // Parse phone numbers
        if (!variantMap.contains("mob")) {
            setError(Response::Property, "The field of \"mob\" is missing");
            return;
        }

        QVariant phonesVariant = variantMap.value("mob");
        if (phonesVariant.type() != QVariant::List) {
            setError(Response::Property, "The value of the field of \"mob\" is incompatible");
            return;
        }

        QList<QString> phoneList;
        QVariantList phoneVariantList = phonesVariant.toList();
        foreach (QVariant item, phoneVariantList) {
            phoneList.append(item.toString());
        }

        // Parse gender
        if (!variantMap.contains("ged")) {
            setError(Response::Property, "The field of \"ged\" is missing");
            return;
        }

        QVariant genderVariant = variantMap.value("ged");
        int gender = genderVariant.toInt(&ok);
        if (!ok)
            return;

        // Parse googleId
        if (!variantMap.contains("gid")) {

        }

        QVariant googleIdVariant = variantMap.value("gid");
        if (googleIdVariant.type() != QVariant::String) {

        }

        QString googleId = googleIdVariant.toString();

        // Parse yahooId
        if (!variantMap.contains("yid")) {

        }

        QVariant yahooIdVariant = variantMap.value("yid");
        if (yahooIdVariant.type() != QVariant::String) {

        }

        QString yahooId = yahooIdVariant.toString();

        // Parse city
        if (!variantMap.contains("cit")) {
            setError(Response::Property, "The field of \"cit\" is missing");
            return;
        }

        QVariant cityVariant = variantMap.value("cit");
        if (cityVariant.type() != QVariant::String) {
            setError(Response::Property, "The value of the field of \"cit\" is incompatible");
            return;
        }

        QString city = cityVariant.toString();

        // Parse birthday
        if (!variantMap.contains("dob")) {
            setError(Response::Property, "The field of \"dob\" is missing");
            return;
        }

        QVariant birthdayVariant = variantMap.value("dob");
        if (birthdayVariant.type() != QVariant::String) {
            setError(Response::Property, "The value of the field of \"dob\" is incompatible");
            return;
        }

        QString birthday = QDateTime(birthdayVariant.toDateTime()).toString("yyyy-MM-dd");

        QString messageStatus = "";

        m_profile = Profile::ProfileData(userId,
                                         userName,
                                         displayName,
                                         firstName,
                                         lastName,
                                         birthday,
                                         gender,
                                         messageStatus,
                                         phoneList,
                                         email,
                                         profileUrl,
                                         avatarUrl,
                                         googleId,
                                         yahooId,
                                         city);

    }
}
